<template>
  <el-dialog
    :title="title"
    :visible.sync="show"
    width="700px"
    append-to-body
    center
  >
    <div class="btns">
      <el-button type="success" icon="el-icon-plus" @click="showAdd = true;addType='same'">同级新增</el-button>
      <el-button type="success" icon="el-icon-plus" @click="showAdd = true;addType='under'">下级新增</el-button>
      <el-button type="primary" icon="el-icon-edit" @click="showAdd = true;addType=''">修改</el-button>
      <el-button type="danger" icon="el-icon-delete" @click="deleteSupplierCategory">删除</el-button>
    </div>
    <div class="search-box">
      <el-input v-model="keyword" /> <el-button style="margin-left: 10px;" type="primary" @click="showAdd = true;addType=''">搜索</el-button>
    </div>
    <div class="dirList">
      <tree-box v-model="selectedNode" :data="treeData" :no-search="true" title="目录" />
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="show = false">确 定</el-button>
    </span>
    <el-dialog
      :title="'新增' + title"
      :visible.sync="showAdd"
      width="500px"
      center
      append-to-body
    >
      <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="100px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="说明" prop="description">
          <el-input v-model="form.description" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showAdd = false">取 消</el-button>
        <el-button type="primary" @click="saveSupplierCategor">确 定</el-button>
      </span>
    </el-dialog>
  </el-dialog>
</template>

<script>
import { getSupplierCategoryTree, addSupplierCategory, deleteSupplierCategory, updateSupplierCategory } from '@/api/travellingTraderManage'
export default {
  name: 'DirManage',
  props: {
    title: {
      type: String,
      default: ''
    },
    type: {
      type: String,
      default: ''
    },
    showModal: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      treeData: [],
      keyword: '',
      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入文件夹名称', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    }
  },
  watch: {
    type: {
      handler() {
        this.getTreeList()
      },
      immediate: true
    }
  },
  methods: {
    getTreeList() {
      getSupplierCategoryTree({ supplierCategoryType: this.type }).then(res => {
        if (res.data.nodes) {
          this.treeData = res.data.nodes
        }
      })
    },
    deleteSupplierCategory() {
      if (!this.selectedNode) {
        return
      }
      this.$confirm('确定要删除该条数据吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const { id } = this.selectedNode
        deleteSupplierCategory({
          supplierCategoryId: id
        }).then(res => {
          const { code, msg } = res
          if (code === '0001') {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getTreeList()
            this.$emit('freshTree')
          } else {
            this.$message({
              type: 'error',
              message: msg || '删除失败!'
            })
          }
        })
      }).catch(() => {
      })
    },
    saveSupplierCategor() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let params = {
            ...this.form,
            type: this.type
          }
          if (!this.selectedNode) {
            params.parentId = null
          } else {
            const { id, parentId } = this.selectedNode
            const createId = this.addType === 'same' ? (parentId || null) : id
            params = {
              ...params,
              parentId: this.addType ? createId : parentId,
              ...!this.addType ? { id } : {}
            }
          }
          const fn = this.addType ? addSupplierCategory : updateSupplierCategory
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.showAdd = false
              this.getTreeList()
              this.$emit('freshTree')
              this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dirList{
  max-height: 500px;
  min-height: 200px;
}
.search-box {
  padding-top: 10px;
  display: flex;
  width: 310px;
}

</style>
